package com.pac.role.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.common.tools.StringTools;
import com.common.util.ErrorNumber;
import com.common.util.LogicException;
import com.pac.role.bean.RoleMenuBean;
import com.pac.role.dao.RoleMenuDao;
import com.pac.role.service.RoleMenuService;

/**
 * 角色菜单关联关系service实现
 * 
 * @author lujun.chen
 * @version [版本号, 2015年2月16日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Service(value = "roleMenuService")
public class RoleMenuServiceImpl implements RoleMenuService {

	private static Logger logger = Logger.getLogger(RoleMenuServiceImpl.class);

	@Autowired
	private RoleMenuDao roleMenuDao;
	
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean addRoleMenu(RoleMenuBean roleMenuBean) {
		if (null == roleMenuBean) {
			logger.error("添加角色菜单关联关系出错，参数为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "参数为空");
		}
		
		if(StringTools.isNull(roleMenuBean.getMenuId()))
		{
			logger.error("添加角色菜单关联关系出错，菜单ID为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL,"菜单ID为空");
		}
		
		if(StringTools.isNull(roleMenuBean.getRoleId()))
		{
			logger.error("添加角色菜单关联关系出错，角色ID为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL,"角色ID为空");
		}
		
		return roleMenuDao.addRoleMenu(roleMenuBean);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean delRoleMenu(RoleMenuBean roleMenuBean) {
		if(null==roleMenuBean)
		{
			logger.error("删除角色菜单关联关系出错，参数为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL,"参数为空");
		}
		
		if(StringTools.isNull(roleMenuBean.getRoleId()))
		{
			logger.error("删除角色菜单关联关系出错，角色ID为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL,"角色ID为空");
		}
		
		return roleMenuDao.delRoleMenu(roleMenuBean);
	}

	@Override
	public List<RoleMenuBean> queryRoleMenu(RoleMenuBean roleMenuBean) {
		return roleMenuDao.queryRoleMenu(roleMenuBean);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean delRoleMenuByRoleId(String roleId) {
		if(StringTools.isNull(roleId))
        {
            logger.error("根据角色ID删除角色菜单关联关系失败，角色ID为空");
            throw new LogicException(ErrorNumber.PARAM_IS_NULL,"根据角色ID删除角色菜单关联关系失败，角色ID为空");
        }
        
        RoleMenuBean roleMenuBean=new RoleMenuBean();
        roleMenuBean.setRoleId(roleId);
        
        
        return this.delRoleMenu(roleMenuBean);
	}
}
